from common.base_z import Base, By, get_driver


class Addproduct(Base):
    """封装表现层:制作每个元素的定位器"""
    # 登录页面元素定位
    close_X = (By.XPATH, '//div[@class="panel-cross"]/span')
    button_loc = (By.ID, 'cloudLogin')
    username_loc = (By.NAME, 'username')
    password_loc = (By.NAME, 'password')
    login_loc = (By.CLASS_NAME, 'btn-a')
    # 商品分类页面元素定位
    merchandise_management_loc = (By.XPATH, '//ul[@id="menu-ul"]/li[2]')  # 定位商品管理元素
    add_products_loc = (By.ID, 'sub-menu-02_goods_add')  # 定位添加新商品元素
    main_frame_loc = (By.ID, 'main-frame')
    goods_name_loc = (By.NAME, 'goods_name')  # 定位商品名称输入框
    product_select_loc = (By.NAME, 'cat_id')  # 定位商品分类下拉框
    product_select_1 = (By.XPATH, '//*[@id="general-table"]/tbody/tr[3]/td[2]/select/option[2]')  # 定位家用电器
    shop_price_loc = (By.NAME, 'shop_price')  # 定位本店售价输入框
    confirm_loc = (By.XPATH, '//div[@class="button-div"]/input[2]')  # 定位确认按钮

   # 封装操作层

    def click_close_x(self):  # 点击关闭激活系统
        self.click(self.close_X)

    def click_button(self):  # 点击其他登录方式按钮
        self.click(self.button_loc)

    def input_username(self, username):  # 输入用户名  username:用户名
        self.send_keys(self.username_loc, username)

    def input_password(self, password):  # 输入密码  password:密码
        self.send_keys(self.password_loc, password)

    def click_login(self):  # 点击登录按钮
        self.click(self.login_loc)

    def merchandise_iframe(self):  # 进入左边导航栏的iframe
        self.iframe(1)

    def merchandise_management(self):  # 点击商品管理
        self.click(self.merchandise_management_loc)

    def add_products(self):  # 点击商品列表
        self.click(self.add_products_loc)

    def quit_iframe(self):  # 退出当前iframe
        self.default_iframe()

    def content_iframe(self):  # 进入新的iframe
        self.iframe_element(self.main_frame_loc)

    def goods_name(self, name):  # 输入商品名称  name：商品名称
        self.send_keys(self.goods_name_loc, name)

    def product_select(self):  # 商品分类下拉框选择
        self.select_index(self.product_select_loc, 1)

    def select_1(self):
        self.click(self.product_select_1)

    def shop_price(self, price):  # 在本店售价输入框输入价格
        self.clear(self.shop_price_loc)
        self.send_keys(self.shop_price_loc, price)

    def confirm(self):  # 点击确认按钮
        self.click(self.confirm_loc)


if __name__ == '__main__':
    product = Addproduct(get_driver())
    product.get('http://localhost:8080/ecshop/admin/privilege.php?act=login')
    product.click_close_x()
    product.click_button()
    product.input_username('root1234')
    product.input_password('root1234')
    product.click_login()
    product.merchandise_iframe()
    product.merchandise_management()
    product.add_products()
    product.quit_iframe()
    product.content_iframe()
    product.goods_name('相机')
    product.product_select()
    product.select_1()
    product.shop_price('20')
    product.confirm()